package cn.iocoder.yudao.module.trade.controller.admin.order;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.pay.api.order.PayOrderApi;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderRespDTO;
import cn.iocoder.yudao.module.trade.controller.admin.order.vo.*;
import cn.iocoder.yudao.module.trade.convert.order.TradeOrderConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDeliveryDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderLogDO;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderLogService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderQueryService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderUpdateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 贵金属交易订单")
@RestController
@RequestMapping("/trade/order")
@Validated
public class TradeOrderQueryController {

    @Resource
    private TradeOrderService orderService;

    @Resource
    private TradeOrderUpdateService tradeOrderUpdateService;
    @Resource
    private TradeOrderQueryService tradeOrderQueryService;
    @Resource
    private PayOrderApi payOrderApi;
    @Resource
    private TradeOrderLogService tradeOrderLogService;

    @Resource
    private MemberUserApi memberUserApi;

    @PostMapping("/settle-info")
    @Operation(summary = "获取结算信息")
    @PreAuthorize("@ss.hasPermission('trade:order:update')")
    public CommonResult<PayOrderCreateReqDTO> getSettleInfo(@Valid @RequestBody TradeOrderSettleReqVO settleReqVO) {
        return success(tradeOrderUpdateService.getSettleInfo(settleReqVO.getOrderNo(),
                settleReqVO.getOrderWeight(), settleReqVO.getIsViolate()));
    }

    @GetMapping("/summary")
    @Operation(summary = "获得交易订单统计")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<TradeOrderSummaryRespVO> getOrderSummary(@RequestParam("type") String type) {
        return success(tradeOrderQueryService.getOrderSummary(type));
    }

    @GetMapping("/index-num")
    @Operation(summary = "获得交易订单统计")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<Map<String, Object>> getIndexNumber() {
        return success(tradeOrderQueryService.getIndexNumber());
    }
    @GetMapping("/index-order-num")
    @Operation(summary = "获得交易订单统计")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<Map<String, Object>> getIndexOrderNumber(@RequestParam("type") Integer type) {
        return success(tradeOrderQueryService.getIndexOrderNumber(type));
    }

    @GetMapping("/get-order-delivery")
    @Operation(summary = "获得交易订单的物流单")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<List<TradeOrderDeliveryDO>> getOrderDelivery(@RequestParam("orderNo") String orderNo, @RequestParam("type") Integer type) {
        return success(tradeOrderQueryService.getOrderDelivery(orderNo,type));
    }

    @GetMapping("/get-express-track-list")
    @Operation(summary = "获得交易订单的物流轨迹")
    @Parameter(name = "id", description = "交易订单编号")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<List<?>> getOrderExpressTrackList(@RequestParam("id") Long id) {
        return success(TradeOrderConvert.INSTANCE.convertList02(
                tradeOrderQueryService.getExpressTrackList(id)));
    }

    @GetMapping("/get")
    @Operation(summary = "获得贵金属交易订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-metals:query')")
    public CommonResult<TradeOrderDetailRespVO> getOrder(@RequestParam("id") String id) {
        TradeOrderDO order = orderService.getOrderMetals(id);
        PayOrderRespDTO payOrder = payOrderApi.getOrder(order.getPayOrderId());
        List<TradeOrderLogDO> logList = tradeOrderLogService.getOrderLogListByOrderNo(order.getOrderNo());
        return success(TradeOrderConvert.INSTANCE.convert(order,payOrder,logList));
    }
    @GetMapping("/get-by-pay-id")
    @Operation(summary = "获得贵金属交易订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-metals:query')")
    public CommonResult<TradeOrderRespVO> getOrderByPayId(@RequestParam("id") String id) {
        TradeOrderDO order = tradeOrderQueryService.getOrderByPayOrderId(Long.valueOf(id));
        return success(BeanUtils.toBean(order, TradeOrderRespVO.class));
    }
    @GetMapping("/get-by-pick-up-verify-code")
    @Operation(summary = "获得贵金属交易订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-metals:query')")
    public CommonResult<TradeOrderDetailRespVO> getOrderByPickUpVerify(@RequestParam("pickUpVerifyCode") String pickUpVerifyCode) {
        TradeOrderDO order = orderService.getOrderByPickUpVerify(pickUpVerifyCode);
        PayOrderRespDTO payOrder = payOrderApi.getOrder(order.getPayOrderId());
        List<TradeOrderLogDO> logList = tradeOrderLogService.getOrderLogListByOrderNo(order.getOrderNo());
        return success(TradeOrderConvert.INSTANCE.convert(order,payOrder,logList));
    }

    @GetMapping("/page")
    @Operation(summary = "获得贵金属交易订单分页")
    @PreAuthorize("@ss.hasPermission('trade:order-metals:query')")
    public CommonResult<PageResult<TradeOrderRespVO>> getOrderMetalsPage(@Valid TradeOrderPageReqVO pageReqVO) {
        PageResult<TradeOrderDO> pageResult = tradeOrderQueryService.getOrderPage(null, pageReqVO);
        return success(BeanUtils.toBean(pageResult, TradeOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出贵金属交易订单 Excel")
    @PreAuthorize("@ss.hasPermission('trade:order-metals:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderMetalsExcel(@Valid TradeOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TradeOrderDO> list = orderService.getOrderMetalsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "贵金属交易订单.xls", "数据", TradeOrderRespVO.class,
                        BeanUtils.toBean(list, TradeOrderRespVO.class));
    }

}